#ifndef __ASM_CMPXCHG_H
#define __ASM_CMPXCHG_H

#include <asm/types.h>
#include <asm/barrier.h>

static inline long cmpxchg(long *ptr, long old, long new)
{
	long oldval;
	long res;

	smp_mb();
	asm volatile("// cmpxchg\n"
		"1:	ldxr	%1, %2\n"
		"	cmp	%1, %3\n"
		"	b.ne	2f\n"
		"	stxr	%w0, %4, %2\n"
		"	cbnz	%w0, 1b\n"
		"2:"
			: "=&r" (res), "=&r" (oldval), "+Q" (ptr)
			: "Ir" (old), "r" (new)
			: "cc");

	smp_mb();
	return oldval;
}


#endif	/* __ASM_CMPXCHG_H */
